const { defineConfig } = require("@vue/cli-service");
const data = require("./src/mock");
const bodyParser = require("body-parser");
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    setupMiddlewares(middle, { app }) {
      app.use(bodyParser.json());
      app.get("/api/list", (req, res) => {
        res.send({
          code: 200,
          data: data.list,
        });
      });
      app.post("/api/edit", (req, res) => {
        const { item } = req.body;
        const result = data.list.find((v) => v.id === item.id);

        if (result) {
          Object.assign(result, item);
          res.send({
            code: 200,
            message: "修改成功！！",
          });
        } else {
          res.send({
            code: 400,
            message: "修改失败",
          });
        }
      });

      app.post("/api/list/add", (req, res) => {
        const { item } = req.body;
        item.id = Math.random().toString().slice(2)

        data.list.unshift(item);
        res.send({
          code: 200
        })
      });
      return middle;
    },
  },
});
